package cn.iocoder.yudao.module.erp.controller.admin.invoicechecklist;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.erp.controller.admin.invoicechecklist.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.invoicechecklist.InvoiceCheckListDO;
import cn.iocoder.yudao.module.erp.service.invoicechecklist.InvoiceCheckListService;

@Tag(name = "管理后台 - ERP 采购发票审核单")
@RestController
@RequestMapping("/erp/invoice-check-list")
@Validated
public class InvoiceCheckListController {

    @Resource
    private InvoiceCheckListService invoiceCheckListService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP 采购发票审核单")
    @PreAuthorize("@ss.hasPermission('erp:invoice-check-list:create')")
    public CommonResult<Long> createInvoiceCheckList(@Valid @RequestBody InvoiceCheckListSaveReqVO createReqVO) {
        return success(invoiceCheckListService.createInvoiceCheckList(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP 采购发票审核单")
    @PreAuthorize("@ss.hasPermission('erp:invoice-check-list:update')")
    public CommonResult<Boolean> updateInvoiceCheckList(@Valid @RequestBody InvoiceCheckListSaveReqVO updateReqVO) {
        invoiceCheckListService.updateInvoiceCheckList(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP 采购发票审核单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:invoice-check-list:delete')")
    public CommonResult<Boolean> deleteInvoiceCheckList(@RequestParam("id") Long id) {
        invoiceCheckListService.deleteInvoiceCheckList(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP 采购发票审核单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:invoice-check-list:query')")
    public CommonResult<InvoiceCheckListRespVO> getInvoiceCheckList(@RequestParam("id") Long id) {
        InvoiceCheckListDO invoiceCheckList = invoiceCheckListService.getInvoiceCheckList(id);
        return success(BeanUtils.toBean(invoiceCheckList, InvoiceCheckListRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP 采购发票审核单分页")
    @PreAuthorize("@ss.hasPermission('erp:invoice-check-list:query')")
    public CommonResult<PageResult<InvoiceCheckListRespVO>> getInvoiceCheckListPage(@Valid InvoiceCheckListPageReqVO pageReqVO) {
        PageResult<InvoiceCheckListDO> pageResult = invoiceCheckListService.getInvoiceCheckListPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, InvoiceCheckListRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP 采购发票审核单 Excel")
    @PreAuthorize("@ss.hasPermission('erp:invoice-check-list:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportInvoiceCheckListExcel(@Valid InvoiceCheckListPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<InvoiceCheckListDO> list = invoiceCheckListService.getInvoiceCheckListPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP 采购发票审核单.xls", "数据", InvoiceCheckListRespVO.class,
                        BeanUtils.toBean(list, InvoiceCheckListRespVO.class));
    }

//    根据发票审核单编号 修改发票审核单状态
    @PutMapping("/update-status-by-check-invoice-id")
    @Operation(summary = "根据发票审核单编号 修改发票审核单状态")
    @PreAuthorize("@ss.hasPermission('erp:invoice-check-list:update')")
    public CommonResult<Boolean> updateStatusByCheckInvoiceId(@RequestParam("checkInvoiceId") String checkInvoiceId,
                                                              @RequestParam("invoiceStatus") String invoiceStatus) {
        invoiceCheckListService.updateStatusByCheckInvoiceId(checkInvoiceId, invoiceStatus);
        return success(true);
    }
    @PutMapping("/update-pay-status-by-check-invoice-id")
    @Operation(summary = "根据发票审核单编号 修改打款审核状态")
    @PreAuthorize("@ss.hasPermission('erp:invoice-check-list:update')")
    public CommonResult<Boolean> updatePayStatusByCheckInvoiceId(@RequestParam("checkInvoiceId") String checkInvoiceId,
                                                              @RequestParam("payStatus") String payStatus) {
        invoiceCheckListService.updatePayStatusByCheckInvoiceId(checkInvoiceId, payStatus);
        return success(true);
    }

}